import { reject } from "lodash";
import { Column, TableData } from "../types";
let metatable: { cached: boolean, fetching: boolean, data: TableData[] } = { cached: false, fetching: false, data: [] }
import mitt from "mitt";
const emitter = mitt();
const event: Record<string, string> = {
    metatableReady: "metatableReady",
}

export function setMetaTable(data: TableData): Promise<any> {
    if (data.table_id) {
        return updateMetaTable(data);
    } else {
        return addMetaTable(data);
    }
}
export function addMetaTable(data: TableData): Promise<any> {
    const url = '/meta/table/add';
    return new Promise((resolve, reject) => {
        window.$theia.http
            .post(url, data)
            .then((response: any) => {
                metatable.cached = false;
                resolve(response);
            })
            .catch((err: any) => {
                reject(err);
            });
    });
}
export function updateMetaTable(data: TableData): Promise<any> {
    const url = '/meta/table/update';
    return new Promise((resolve, reject) => {
        window.$theia.http
            .post(url, data)
            .then((response: any) => {
                metatable.cached = false;
                resolve(response);
            })
            .catch((err: any) => {
                reject(err);
            });
    });
}


export function setMetaColumn(data: Column): Promise<any> {
    if (data.column_id) {
        return updateMetaColumn(data);
    } else {
        return addMetaColumn(data);
    }
}

export function addMetaColumn(data: Column): Promise<any> {
    const url = '/meta/column/add';
    return new Promise((resolve, reject) => {
        window.$theia.http
            .post(url, data)
            .then((response: any) => {
                metatable.cached = false;
                resolve(response);
            })
            .catch((err: any) => {
                reject(err);
            });
    });
}
export function updateMetaColumn(data: Column): Promise<any> {
    const url = '/meta/column/update';
    return new Promise((resolve, reject) => {
        window.$theia.http
            .post(url, data)
            .then((response: any) => {
                metatable.cached = false;
                resolve(response);
            })
            .catch((err: any) => {
                reject(err);
            });
    });
}

export function getMetaTable(table_id: any): Promise<any> {
    return new Promise((resolve) => {
        getAllMetaTable().then((data: TableData[]) => {
            resolve(data.find((item: TableData) => item.table_id == table_id));
        }).catch(() => {
            resolve(null);
        });
    });
}
export function getMetaColumn(column_id: any): Promise<any> {
    return new Promise((resolve) => {
        getAllMetaTable().then((data: TableData[]) => {
            const table = data.find((item: TableData) => 
                item.columns.some((column: Column) => column.column_id === column_id)
            );
            // 如果找到了表，从中提取出对应的列
            if (table) {
                const column = table.columns.find((column: Column) => column.column_id === column_id);
                resolve(column);
            } else {
                reject(null);
            }
            reject(null);
        }
        );
    })
}
export function getMetaTableByName(name: string): Promise<any> {
    return new Promise((resolve,reject) => {
        getAllMetaTable().then((data: TableData[]) => {
            resolve(data.find((item: TableData) => item.name == name));
        }).catch(() => {
            reject(null);
        });
    });
}

export function getAllMetaTable(): Promise<any> {
    if (metatable.cached) {
        return new Promise((resolve) => {
            resolve(metatable.data);
        });
    }
    if (metatable.fetching) {
        return new Promise((resolve) => {
            emitter.on(event.metatableReady, () => {
                resolve(metatable.data);
            })
            if (metatable.cached) {
                resolve(metatable.data);
            }
        });
    }
    metatable.fetching = true;
    const url = '/meta/table/all';
    return new Promise((resolve, reject) => {
        window.$theia.http
            .post(url)
            .then((response: any) => {
                metatable.data = response;
                metatable.cached = true;
                metatable.fetching = false;
                emitter.emit(event.metatableReady);//触发事件
                resolve(response);
            })
            .catch((err: any) => {
                metatable.fetching = false;
                reject(err);
            });
    });
}

